package cc.cc4414.spring.resource.config;

import cc.cc4414.spring.common.property.CommonProperties;
import cc.cc4414.spring.resource.util.TokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token过滤器，判断token是否失效
 *
 * @author cc 2019年10月21日
 */
@WebFilter
@RequiredArgsConstructor
public class TokenFilter implements Filter {
    private final StringRedisTemplate redisTemplate;

    private final CommonProperties commonProperties;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (!commonProperties.isMicroservice()) {
            String value = ((HttpServletRequest) request).getHeader(HttpHeaders.AUTHORIZATION);
            if (TokenUtils.isExpires(value, redisTemplate)) {
                ((HttpServletResponse) response).setStatus(HttpStatus.UNAUTHORIZED.value());
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
